﻿using System;
using System.Data;
using System.Collections;
using CE.Database;

/// <summary>
/// Summary description for CXPromoViewDB
/// </summary>
/// 
namespace CE.Promotion
{
    public class CXPromoViewDB
    {
        public CXPromoViewDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CXPromoView cxpromoview)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocCXPromoViewInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@PromoViewId", cxpromoview.PromoViewId);
            _sPara.Add("@IPNumber", cxpromoview.IPNumber);
            _sPara.Add("@BrowserType", cxpromoview.BrowserType);
            _sPara.Add("@EmailAddress", cxpromoview.EmailAddress);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CXPromoView GetSingleItemByIP(string IPNumber)
        {
            MyDatabase _Db = new MyDatabase();
            string _spName = "sprocCXPromoViewSelectSingleItemByIP";
            CXPromoView _result = null;
            DataTable _dt;
            SortedList _sPara = new SortedList();

            _sPara.Add("@IPNumber", IPNumber);

            _dt = _Db.GetDataTableBySP(_spName, _sPara);

            if (_dt != null && _dt.Rows.Count > 0)
            {
                _result = new CXPromoView();
                _result = FillDataRecord(_dt);
            }

            return _result;
        }


        private CXPromoView FillDataRecord(DataTable myDataTable)
        {
            CXPromoView cxpromo = new CXPromoView();
            cxpromo.PromoViewId = Convert.ToInt64(myDataTable.Rows[0]["PromoViewId"].ToString());
            cxpromo.IPNumber = myDataTable.Rows[0]["IPNumber"].ToString();
            cxpromo.BrowserType = myDataTable.Rows[0]["BrowserType"].ToString();
            cxpromo.EmailAddress = myDataTable.Rows[0]["EmailAddress"].ToString();
            return cxpromo;
        } 
    }
}
